#ifndef _CPU_IRQFLAGS_H_
#define _CPU_IRQFLAGS_H_

#include <riscv.h>

#define in_interrupts()            ({unsigned long val;val = read_csr(mstatus); val = val >> 31;!!val;})
#define in_disabled_interrupt()    ({unsigned long val;val = read_csr(mie); val = val & MSTATUS_MIE;!!val;})
#define is_disabled_interrupt()    (in_interrupts() || in_disabled_interrupt())


#define local_irq_disable()                     \
{                                             \
	clear_csr(mstatus, MSTATUS_MIE);            \
}

#define local_irq_enable()                      \
{                                             \
	set_csr(mstatus, MSTATUS_MIE);              \
}

#define _local_irq_save()  clear_csr(mstatus, MSTATUS_MIE)

#define local_irq_save(flags)                   \
do {                                          \
	flags = _local_irq_save();                  \
} while (0)

static inline void local_irq_restore(unsigned long flags)
{
    set_csr(mstatus, flags & MSTATUS_MIE);
}


#endif /* _CPU_IRQFLAGS_H_ */
